package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.RechargeRecordBO;
import com.junzhiit.nengji.bo.RechargeRecordSearchBO;
import com.junzhiit.nengji.cms.service.RechargeRecordService;
import com.junzhiit.nengji.cms.web.util.OrderNumberUtils;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.RechargeRecordVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.RechargeRecordDAO;
import com.junzhiit.nengji.model.RechargeRecord;

@Service
public class RechargeRecordServiceImpl extends BaseServiceImpl implements RechargeRecordService {
	
	@Autowired
	private RechargeRecordDAO rechargeRecordDAO;
	
	@Override
	public ResultVO add(RechargeRecordVO rc) {
		ResultVO result = new ResultVO();
		RechargeRecord rechargeRecord = new RechargeRecord();
		BeanUtils.copyProperties(rc, rechargeRecord);
		String number = OrderNumberUtils.getRandomNumber();
		rechargeRecord.setOrderNumber(number);
		result.setData(rechargeRecordDAO.insert(rechargeRecord));
		return result;
	}

	@Override
	public Map<String, Object> findAll(RechargeRecordVO vo, DataTablesParamVO dtp) {
		RechargeRecordSearchBO rechargeRecordBO = new RechargeRecordSearchBO();
		BeanUtils.copyProperties(vo, rechargeRecordBO);
		if(rechargeRecordBO.getEndTime()!=null){
			rechargeRecordBO.setEndTime(DateUtil.addDays(rechargeRecordBO.getEndTime(), 1));
		}
		int totalCount = rechargeRecordDAO.getTotalCount(rechargeRecordBO);
		List<RechargeRecordBO> list = rechargeRecordDAO.findAll(rechargeRecordBO, dtp.getStart(), dtp.getLength());
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

}
